package com.bestom.communication.activity;

import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.bestom.communication.R;
import com.bestom.communication.ThisApplication;
import com.bestom.communication.constants.FilesConstant;
import com.bestom.communication.utils.SharedPreferencesUtil;

public class SettingActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
        updateData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.setting, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
            case R.id.update:
                setEnabled(true);
                break;
            case R.id.save:
                new AlertDialog.Builder(this)
                        .setTitle("保存")
                        .setMessage("确定要保存修改吗？")
                        .setPositiveButton("确定", (dialog, which) -> {
                            saveData();
                            updateData();
                            setEnabled(false);
                        })
                        .setNegativeButton("取消", null)
                        .show();
                break;
            case R.id.reset:
                new AlertDialog.Builder(this)
                        .setTitle("恢复默认")
                        .setMessage("确定要恢复默认数据吗？")
                        .setPositiveButton("确定", (dialog, which) -> {
                            resetData();
                            updateData();
                            setEnabled(false);
                        })
                        .setNegativeButton("取消", null)
                        .show();
                break;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    /**保存数据*/
    private void saveData() {
        SharedPreferencesUtil.getInstance().putValue(FilesConstant.mMqttAddressPref,mqttAddress.getText().toString().trim());
        SharedPreferencesUtil.getInstance().putValue(FilesConstant.mPortPref,port.getText().toString().trim());
        SharedPreferencesUtil.getInstance().putValue(FilesConstant.mClientIdPref,clientId.getText().toString().trim());
        SharedPreferencesUtil.getInstance().putValue(FilesConstant.mUsernamePref,username.getText().toString().trim());
        SharedPreferencesUtil.getInstance().putValue(FilesConstant.mPasswordPref,password.getText().toString().trim());
        SharedPreferencesUtil.getInstance().putValue(FilesConstant.mTimePref,time.getText().toString().trim());
        ThisApplication.getApplication().setTime(Integer.parseInt(time.getText().toString().trim()));
    }

    /**恢复默认*/
    private void resetData() {
        SharedPreferencesUtil.getInstance().putValue(FilesConstant.mMqttAddressPref,FilesConstant.mDefaultMqttAddress);
        SharedPreferencesUtil.getInstance().putValue(FilesConstant.mPortPref,FilesConstant.mDefaultPort);
        SharedPreferencesUtil.getInstance().putValue(FilesConstant.mClientIdPref,FilesConstant.mDefaultClientId);
        SharedPreferencesUtil.getInstance().putValue(FilesConstant.mUsernamePref,FilesConstant.mDefaultUsername);
        SharedPreferencesUtil.getInstance().putValue(FilesConstant.mPasswordPref,FilesConstant.mDefaultPassword);
        SharedPreferencesUtil.getInstance().putValue(FilesConstant.mTimePref,FilesConstant.mDefaultTime);
    }

    /**更新视图*/
    private void updateData() {
        mqttAddress.setText((String) SharedPreferencesUtil.getInstance().getValue(FilesConstant.mMqttAddressPref,FilesConstant.mDefaultMqttAddress));
        port.setText((String) SharedPreferencesUtil.getInstance().getValue(FilesConstant.mPortPref,FilesConstant.mDefaultPort));
        clientId.setText((String) SharedPreferencesUtil.getInstance().getValue(FilesConstant.mClientIdPref,FilesConstant.mDefaultClientId));
        username.setText((String) SharedPreferencesUtil.getInstance().getValue(FilesConstant.mUsernamePref,FilesConstant.mDefaultUsername));
        password.setText((String) SharedPreferencesUtil.getInstance().getValue(FilesConstant.mPasswordPref,FilesConstant.mDefaultPassword));
        time.setText((String) SharedPreferencesUtil.getInstance().getValue(FilesConstant.mTimePref,FilesConstant.mDefaultTime));
    }

    /**设置是否可点击*/
    private void setEnabled(boolean b) {
        mqttAddress.setEnabled(b);
        port.setEnabled(b);
        clientId.setEnabled(b);
        username.setEnabled(b);
        password.setEnabled(b);
        time.setEnabled(b);
        int color;
        if (b) color = Color.WHITE;
        else color = Color.parseColor("#09ebb3");
        mqttAddress.setTextColor(color);
        port.setTextColor(color);
        clientId.setTextColor(color);
        username.setTextColor(color);
        password.setTextColor(color);
        time.setTextColor(color);
    }

    private EditText mqttAddress;
    private EditText port;
    private EditText clientId;
    private EditText username;
    private EditText password;
    private EditText time;
    private void initView() {
        mqttAddress = findViewById(R.id.mqtt_address);
        port = findViewById(R.id.port);
        clientId = findViewById(R.id.client_id);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        time = findViewById(R.id.time);
    }
}
